/*
   Copyright 2011-2012 gtalent2@gmail.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package combat

import (
	"../entity"
	"github.com/gtalent/golua"
	"github.com/gtalent/starfish/gfx"
	"github.com/gtalent/starfish/util"
	"math/rand"
)

type creature struct {
	*entity.Creature
	frontAnim         *gfx.Animation
	backAnim          *gfx.Animation
	movesUsed         []byte
	moveDisabled      [4]bool
	attackMod         statMod
	defenseMod        statMod
	specialAttackMod  statMod
	specialDefenseMod statMod
	accuracyMod       statMod
	evasionMod        statMod
	speedMod          statMod
}

func CreatureCombatant(c *entity.Creature) Combatant {
	out := new(creature)
	out.Creature = c
	out.frontAnim = c.FrontView
	out.backAnim = c.BackView

	out.attackMod = new_statMod()
	out.defenseMod = new_statMod()
	out.specialAttackMod = new_statMod()
	out.specialDefenseMod = new_statMod()
	out.accuracyMod = new_statMod()
	out.evasionMod = new_statMod()
	out.speedMod = new_statMod()

	return out
}

func (me *creature) deployCreature() {
}

func (me *creature) takeTurn(c *entity.Creature) {
}

func (me *creature) recallCreature() {
}

func (me *creature) front() (func(c *gfx.Canvas, x, y int), util.Size) {
	i := me.frontAnim.GetImage()
	return func(c *gfx.Canvas, x, y int) { c.DrawImage(i, x, y) }, i.Size()
}

func (me *creature) back() (func(c *gfx.Canvas, x, y int), util.Size) {
	i := me.backAnim.GetImage()
	return func(c *gfx.Canvas, x, y int) { c.DrawImage(i, x, y) }, i.Size()
}

func (me *creature) current() *creature {
	return me
}

func (me *creature) name() string {
	return me.Name
}

func (me *creature) attack() int {
	return int(float64(me.Attack) * me.attackMod.mod)
}

func (me *creature) defense() int {
	return int(float64(me.Defense) * me.defenseMod.mod)
}

func (me *creature) speed() int {
	return int(float64(me.Speed) * me.speedMod.mod)
}

func (me *creature) specialAttack() int {
	return int(float64(me.SpecAttack) * me.specialAttackMod.mod)
}

func (me *creature) specialDefense() int {
	return int(float64(me.SpecDefense) * me.specialDefenseMod.mod)
}

func (me *creature) modAttack(mod int) {
	me.attackMod.standardMod(mod)
}

func (me *creature) modDefense(mod int) {
	me.defenseMod.standardMod(mod)
}

func (me *creature) modSpecialAttack(mod int) {
	me.specialAttackMod.standardMod(mod)
}

func (me *creature) modSpecialDefense(mod int) {
	me.specialDefenseMod.standardMod(mod)
}

func (me *creature) modSpeed(mod int) {
	me.speedMod.standardMod(mod)
}

func (me *creature) modEvasion(mod int) {
	me.evasionMod.level += mod
	me.evasionMod.evAccMod(me.evasionMod.level)
}

func (me *creature) modAccuracy(mod int) {
	me.accuracyMod.level += mod
	me.accuracyMod.evAccMod(-me.accuracyMod.level)
}

func (me *creature) attackOpponent(op *creature, mov entity.CreatureMove) {
	l := float64(me.Level)
	a := float64(me.attack())
	b := float64(mov.Power)
	d := float64(op.defense())
	if mov.Special {
		d = float64(op.specialDefense())
	}
	s := float64(1)
	for _, a := range me.Types {
		if a.Name == mov.Type.Name {
			s = 1.5
			break
		}
	}
	e := float64(0)
	r := float64(rand.Int()%38 + 237)
	dam := int(((((2*l/5 + 2) * a * b / d) / 50) + 2) * s * e * r / 255)
	op.Health.Current -= dam
	if op.Health.Current < 0 {
		op.Health.Current = 0
	}
	if mov.Script != "" {
		l := me.luaInstance(op)
		l.DoString(mov.Script)
		l.Close()
	}
}

func (me *creature) luaInstance(op *creature) *golua.State {
	l := golua.NewState()
	getInt := func(name string, v func() int) {
		l.Register(name, func(l *golua.State) int {
			l.PushInteger(v())
			return 1
		})
	}
	setInt := func(name string, f func() *int) {
		l.Register(name, func(l *golua.State) int {
			v := f()
			*v = l.ToInteger(0)
			return 0
		})
	}
	modStat := func(name string, f func() *statMod) {
		l.Register(name, func(l *golua.State) int {
			f().standardMod(l.ToInteger(0))
			return 0
		})
	}
	var tar *creature
	l.OpenLibs()
	l.Register("loadMe", func(l *golua.State) int {
		tar = me
		return 0
	})
	l.Register("loadOpponent", func(l *golua.State) int {
		tar = op
		return 0
	})
	l.Register("movesUsed", func(l *golua.State) int {
		roundsAgo := l.ToInteger(0) + 1
		l.PushInteger(int(me.movesUsed[len(me.movesUsed)-roundsAgo]))
		return 1
	})
	getInt("movesUsedLen", func() int { return len(me.movesUsed) })
	getInt("level", func() int { return tar.Level })
	getInt("maxHP", func() int { return tar.Health.Available })
	getInt("currentHP", func() int { return tar.Health.Current })
	setInt("setHP", func() *int { return &tar.Health.Available })

	getInt("attack", func() int { return tar.attack() })
	getInt("defense", func() int { return tar.defense() })
	getInt("specialAttack", func() int { return tar.specialAttack() })
	getInt("specialDefense", func() int { return tar.specialDefense() })
	getInt("speed", func() int { return tar.speed() })

	modStat("modAttack", func() *statMod { return &tar.attackMod })
	modStat("modDefense", func() *statMod { return &tar.defenseMod })
	modStat("modSpecialAttack", func() *statMod { return &tar.specialAttackMod })
	modStat("modSpecialDefense", func() *statMod { return &tar.specialDefenseMod })
	modStat("modSpeed", func() *statMod { return &tar.speedMod })
	l.Register("modEvasion", func(l *golua.State) int {
		mod := l.ToInteger(0)
		tar.evasionMod.level += mod
		if tar.evasionMod.level < -6 {
			tar.evasionMod.level = -6
		} else if mod > 6 {
			tar.evasionMod.level = 6
		}
		tar.evasionMod.evAccMod(tar.evasionMod.level)
		return 0
	})
	l.Register("modAccuracy", func(l *golua.State) int {
		mod := l.ToInteger(0)
		tar.accuracyMod.level += mod
		if tar.accuracyMod.level < -6 {
			tar.accuracyMod.level = -6
		} else if mod > 6 {
			tar.accuracyMod.level = 6
		}
		tar.accuracyMod.evAccMod(-tar.accuracyMod.level)
		return 0
	})

	l.Register("disableMove", func(l *golua.State) int {
		tar.moveDisabled[l.ToInteger(0)] = true
		return 0
	})
	l.Register("reducePP", func(l *golua.State) int {
		mov := l.ToInteger(0)
		by := l.ToInteger(1)
		tar.Moves[mov].PP.Current -= by
		return 0
	})

	return l
}

func (me *creature) processStatusCond() {

}

func (me *creature) free() {
	me.frontAnim.Free()
	me.backAnim.Free()
}
